home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / getpath.c < prev    next >
C/C++ Source or Header  |  1999-04-26  |  19KB  |  744 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Return the initial module search path. */
  33.  
  34. #include "Python.h"
  35. #include "osdefs.h"
  36.  
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <string.h>
  40.  
  41. #ifdef _AMIGA
  42. #include <proto/dos.h>
  43. #endif
  44.  
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif /* HAVE_UNISTD_H */
  48.  
  49. #ifdef WITH_NEXT_FRAMEWORK
  50. #include <mach-o/dyld.h>
  51. #endif
  52.  
  53. #include "protos/getpath.h"
  54.  
  55. /* Search in some common locations for the associated Python libraries.
  56.  *
  57.  * Two directories must be found, the platform independent directory
  58.  * (prefix), containing the common .py and .pyc files, and the platform
  59.  * dependent directory (exec_prefix), containing the shared library
  60.  * modules.  Note that prefix and exec_prefix can be the same directory,
  61.  * but for some installations, they are different.
  62.  *
  63.  * Py_GetPath() carries out separate searches for prefix and exec_prefix.
  64.  * Each search tries a number of different locations until a ``landmark''
  65.  * file or directory is found.  If no prefix or exec_prefix is found, a
  66.  * warning message is issued and the preprocessor defined PREFIX and
  67.  * EXEC_PREFIX are used (even though they will not work); python carries on
  68.  * as best as is possible, but most imports will fail.
  69.  *
  70.  * Before any searches are done, the location of the executable is
  71.  * determined.  If argv[0] has one or more slashs in it, it is used
  72.  * unchanged.  Otherwise, it must have been invoked from the shell's path,
  73.  * so we search $PATH for the named executable and use that.  If the
  74.  * executable was not found on $PATH (or there was no $PATH environment
  75.  * variable), the original argv[0] string is used.
  76.  *
  77.  * Next, the executable location is examined to see if it is a symbolic
  78.  * link.  If so, the link is chased (correctly interpreting a relative
  79.  * pathname if one is found) and the directory of the link target is used.
  80.  *
  81.  * Finally, argv0_path is set to the directory containing the executable
  82.  * (i.e. the last component is stripped).
  83.  *
  84.  * With argv0_path in hand, we perform a number of steps.  The same steps
  85.  * are performed for prefix and for exec_prefix, but with a different
  86.  * landmark.
  87.  *
  88.  * Step 1. Are we running python out of the build directory?  This is
  89.  * checked by looking for a different kind of landmark relative to
  90.  * argv0_path.  For prefix, the landmark's path is derived from the VPATH
  91.  * preprocessor variable (taking into account that its value is almost, but
  92.  * not quite, what we need).  For exec_prefix, the landmark is
  93.  * Modules/Setup.  If the landmark is found, we're done.
  94.  *
  95.  * For the remaining steps, the prefix landmark will always be
  96.  * lib/python$VERSION/string.py and the exec_prefix will always be
  97.  * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
  98.  * number as supplied by the Makefile.  Note that this means that no more
  99.  * build directory checking is performed; if the first step did not find
  100.  * the landmarks, the assumption is that python is running from an
  101.  * installed setup.
  102.  *
  103.  * Step 2. See if the $PYTHONHOME environment variable points to the
  104.  * installed location of the Python libraries.  If $PYTHONHOME is set, then
  105.  * it points to prefix and exec_prefix.  $PYTHONHOME can be a single
  106.  * directory, which is used for both, or the prefix and exec_prefix
  107.  * directories separated by a colon.
  108.  *
  109.  * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
  110.  * backtracking up the path until it is exhausted.  This is the most common
  111.  * step to succeed.  Note that if prefix and exec_prefix are different,
  112.  * exec_prefix is more likely to be found; however if exec_prefix is a
  113.  * subdirectory of prefix, both will be found.
  114.  *
  115.  * Step 4. Search the directories pointed to by the preprocessor variables
  116.  * PREFIX and EXEC_PREFIX.  These are supplied by the Makefile but can be
  117.  * passed in as options to the configure script.
  118.  *
  119.  * That's it!
  120.  *
  121.  * Well, almost.  Once we have determined prefix and exec_prefix, the
  122.  * preprocesor variable PYTHONPATH is used to construct a path.  Each
  123.  * relative path on PYTHONPATH is prefixed with prefix.  Then the directory
  124.  * containing the shared library modules is appended.  The environment
  125.  * variable $PYTHONPATH is inserted in front of it all.  Finally, the
  126.  * prefix and exec_prefix globals are tweaked so they reflect the values
  127.  * expected by other code, by stripping the "lib/python$VERSION/..." stuff
  128.  * off.  If either points to the build directory, the globals are reset to
  129.  * the corresponding preprocessor variables (so sys.prefix will reflect the
  130.  * installation location, even though sys.path points into the build
  131.  * directory).  This seems to make more sense given that currently the only
  132.  * known use of sys.prefix and sys.exec_prefix is for the ILU installation
  133.  * process to find the installed Python tree.
  134.  */
  135.  
  136. #ifndef VERSION
  137. #define VERSION "1.5"
  138. #endif
  139.  
  140. #ifndef VPATH
  141. #define VPATH "."
  142. #endif
  143.  
  144. #ifndef PREFIX
  145. #define PREFIX "/usr/local"
  146. #endif
  147.  
  148. #ifndef EXEC_PREFIX
  149. #define EXEC_PREFIX PREFIX
  150. #endif
  151.  
  152. #ifndef PYTHONPATH
  153. /* I know this isn't K&R C, but the Makefile specifies it anyway */
  154. #ifdef _AMIGA
  155. #define PYTHONPATH PREFIX "lib/python" VERSION ";" EXEC_PREFIX "lib/python" VERSION "/lib-dynload"
  156. #else /* !_AMIGA */
  157. #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
  158.           EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
  159. #endif
  160. #endif
  161.  
  162. #ifndef LANDMARK
  163. #define LANDMARK "string.py"
  164. #endif
  165.  
  166. static char prefix[MAXPATHLEN+1];
  167. static char exec_prefix[MAXPATHLEN+1];
  168. static char progpath[MAXPATHLEN+1];
  169. static char *module_search_path = NULL;
  170. static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
  171.  
  172. #ifdef _AMIGA
  173. /* determine full program path */
  174. extern void Py_GetArgcArgv Py_PROTO((int *argc, char ***argv));    /* in main.c */
  175.  
  176. static const char *fullprogpath(void)
  177. {
  178.     static char path[MAXPATHLEN*2];
  179.     static char prog[MAXPATHLEN];
  180.  
  181.     BPTR dir;
  182.  
  183.     extern BOOL from_WB;    /* in main.c */
  184.  
  185.     // If the program name contains ':' or '/' it's not in the user's path
  186.     // and probably set by using Py_SetProgramName. In that case, just
  187.     // use this. If it exists!
  188.     strcpy(path,Py_GetProgramName());
  189.     if(strchr(path,':') || strchr(path,'/'))
  190.     {
  191.         if(!isxfile(path))
  192.         {
  193.             // Error; the specified file does not exist or is no exe
  194.             path[0]='\0';
  195.         }
  196.         return path;
  197.     }
  198.  
  199.     // Construct the full path of our executable program.
  200.     if(from_WB)
  201.     {
  202.         /* We're launced from WB, GetProgramName() won't work */
  203.         /* Use WB's argv[0] as the executable path */
  204.         int argc;
  205.         char **argv;
  206.         Py_GetArgcArgv(&argc, &argv);
  207.         if(argc>0)
  208.             strcpy(path,argv[0]);
  209.         else
  210.             strcpy(path,"!error!");
  211.     }
  212.     else
  213.     {
  214.         /* Launced from CLI, use GetProgramName */
  215.  
  216.         /* However, first check if the specified name exists */
  217.         if(!isxfile(path))
  218.         {
  219.             path[0]='\0';
  220.             return path;
  221.         }
  222.  
  223.         path[0]=0;
  224.         if(dir=GetProgramDir())
  225.         {
  226.             (void)NameFromLock(dir,path,MAXPATHLEN);
  227.             if(!GetProgramName(prog,MAXPATHLEN))    // this is a dos.library function!
  228.                 strcpy(prog,"!error!");
  229.             if(!AddPart(path,prog,MAXPATHLEN*2))
  230.                 strcpy(path,"!error!");
  231.         }    
  232.     }
  233.     return path;
  234. }
  235.  
  236. static void reduce( char *dir)
  237. {
  238.     int i = strlen(dir);
  239.     if(i>0 && dir[i-1]==':') dir[--i]=0;
  240.     while (i > 0 && dir[i] != SEP && dir[i] != ':') --i;
  241.     if(dir[i]!=':') dir[i] = '\0';
  242.     else dir[i+1]='\0';
  243. }
  244.  
  245. #else /*! AMIGA */
  246.  
  247. static void
  248. reduce(dir)
  249.     char *dir;
  250. {
  251.     int i = strlen(dir);
  252.     while (i > 0 && dir[i] != SEP)
  253.         --i;
  254.     dir[i] = '\0';
  255. }
  256.  
  257. #endif
  258.  
  259. #ifndef S_ISREG
  260. #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
  261. #endif
  262.  
  263. #ifndef S_ISDIR
  264. #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
  265. #endif
  266.  
  267. static int
  268. isfile(filename)        /* Is file, not directory */
  269.     char *filename;
  270. {
  271.     struct stat buf;
  272.     if (stat(filename, &buf) != 0)
  273.         return 0;
  274.     if (!S_ISREG(buf.st_mode))
  275.         return 0;
  276.     return 1;
  277. }
  278.  
  279.  
  280. static int
  281. ismodule(filename)        /* Is module -- check for .pyc/.pyo too */
  282.     char *filename;
  283. {
  284.     if (isfile(filename))
  285.         return 1;
  286.  
  287.     /* Check for the compiled version of prefix. */
  288.     if (strlen(filename) < MAXPATHLEN) {
  289.         strcat(filename, Py_OptimizeFlag ? "o" : "c");
  290.         if (isfile(filename))
  291.             return 1;
  292.     }
  293.     return 0;
  294. }
  295.  
  296.  
  297. static int
  298. isxfile(filename)        /* Is executable file */
  299.     char *filename;
  300. {
  301.     struct stat buf;
  302.     if (stat(filename, &buf) != 0)
  303.         return 0;
  304.     if (!S_ISREG(buf.st_mode))
  305.         return 0;
  306.     if ((buf.st_mode & 0111) == 0)
  307.         return 0;
  308.     return 1;
  309. }
  310.  
  311.  
  312. static int
  313. isdir(filename)            /* Is directory */
  314.     char *filename;
  315. {
  316.     struct stat buf;
  317.     if (stat(filename, &buf) != 0)
  318.         return 0;
  319.     if (!S_ISDIR(buf.st_mode))
  320.         return 0;
  321.     return 1;
  322. }
  323.  
  324.  
  325. #ifdef _AMIGA
  326. #define joinpath(buffer,stuff) AddPart(buffer,stuff,MAXPATHLEN)
  327. #else
  328. static void
  329. joinpath(buffer, stuff)
  330.     char *buffer;
  331.     char *stuff;
  332. {
  333.     int n, k;
  334.     if (stuff[0] == SEP)
  335.         n = 0;
  336.     else {
  337.         n = strlen(buffer);
  338.         if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
  339.             buffer[n++] = SEP;
  340.     }
  341.     k = strlen(stuff);
  342.     if (n + k > MAXPATHLEN)
  343.         k = MAXPATHLEN - n;
  344.     strncpy(buffer+n, stuff, k);
  345.     buffer[n+k] = '\0';
  346. }
  347. #endif
  348.  
  349.  
  350. static int
  351. search_for_prefix(argv0_path, home)
  352.     char *argv0_path;
  353.     char *home;
  354. {
  355.     int n;
  356.     char *vpath;
  357.  
  358.     /* Check to see if argv[0] is in the build directory */
  359.     strcpy(prefix, argv0_path);
  360.     joinpath(prefix, "Modules/Setup");
  361.     if (isfile(prefix)) {
  362.         /* Check VPATH to see if argv0_path is in the build directory.
  363.          * Complication: the VPATH passed in is relative to the
  364.          * Modules build directory and points to the Modules source
  365.          * directory; we need it relative to the build tree and
  366.          * pointing to the source tree.  Solution: chop off a leading
  367.          * ".." (but only if it's there -- it could be an absolute
  368.          * path) and chop off the final component (assuming it's
  369.          * "Modules").
  370.          */
  371.         vpath = VPATH;
  372.         if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
  373.             vpath += 3;
  374.         strcpy(prefix, argv0_path);
  375.         joinpath(prefix, vpath);
  376.         reduce(prefix);
  377.         joinpath(prefix, "Lib");
  378.         joinpath(prefix, LANDMARK);
  379.         if (ismodule(prefix))
  380.             return -1;
  381.     }
  382.  
  383.     if (home) {
  384.         /* Check $PYTHONHOME */
  385.         char *delim;
  386.         strcpy(prefix, home);
  387.         delim = strchr(prefix, DELIM);
  388.         if (delim)
  389.             *delim = '\0';
  390.         joinpath(prefix, lib_python);
  391.         joinpath(prefix, LANDMARK);
  392.         if (ismodule(prefix))
  393.             return 1;
  394.     }
  395.  
  396.     /* Search from argv0_path, until root is found */
  397.     strcpy(prefix, argv0_path);
  398.     do {
  399.         n = strlen(prefix);
  400.         joinpath(prefix, lib_python);
  401.         joinpath(prefix, LANDMARK);
  402.         if (ismodule(prefix))
  403.             return 1;
  404.         prefix[n] = '\0';
  405.         reduce(prefix);
  406.     } while (prefix[0]);
  407.  
  408.     /* Look at configure's PREFIX */
  409.     strcpy(prefix, PREFIX);
  410.     joinpath(prefix, lib_python);
  411.     joinpath(prefix, LANDMARK);
  412.     if (ismodule(prefix))
  413.         return 1;
  414.  
  415.     /* Fail */
  416.     return 0;
  417. }
  418.  
  419.  
  420. static int
  421. search_for_exec_prefix(argv0_path, home)
  422.     char *argv0_path;
  423.     char *home;
  424. {
  425.     int n;
  426.  
  427.     /* Check to see if argv[0] is in the build directory */
  428.     strcpy(exec_prefix, argv0_path);
  429.     joinpath(exec_prefix, "Modules/Setup");
  430.     if (isfile(exec_prefix)) {
  431.         reduce(exec_prefix);
  432.         return -1;
  433.     }
  434.  
  435.     if (home) {
  436.         /* Check $PYTHONHOME */
  437.         char *delim;
  438.         delim = strchr(home, DELIM);
  439.         if (delim)
  440.             strcpy(exec_prefix, delim+1);
  441.         else
  442.             strcpy(exec_prefix, home);
  443.         joinpath(exec_prefix, lib_python);
  444.         joinpath(exec_prefix, "lib-dynload");
  445.         if (isdir(exec_prefix))
  446.             return 1;
  447.     }
  448.  
  449.     /* Search from argv0_path, until root is found */
  450.     strcpy(exec_prefix, argv0_path);
  451.     do {
  452.         n = strlen(exec_prefix);
  453.         joinpath(exec_prefix, lib_python);
  454.         joinpath(exec_prefix, "lib-dynload");
  455.         if (isdir(exec_prefix))
  456.             return 1;
  457.         exec_prefix[n] = '\0';
  458.         reduce(exec_prefix);
  459.     } while (exec_prefix[0]);
  460.  
  461.     /* Look at configure's EXEC_PREFIX */
  462.     strcpy(exec_prefix, EXEC_PREFIX);
  463.     joinpath(exec_prefix, lib_python);
  464.     joinpath(exec_prefix, "lib-dynload");
  465.     if (isdir(exec_prefix))
  466.         return 1;
  467.  
  468.     /* Fail */
  469.     return 0;
  470. }
  471.  
  472.  
  473. static void
  474. calculate_path()
  475. {
  476.     extern char *Py_GetProgramName();
  477.  
  478.     static char delimiter[2] = {DELIM, '\0'};
  479.     static char separator[2] = {SEP, '\0'};
  480.     char *pythonpath = PYTHONPATH;
  481.     char *rtpypath = getenv("PYTHONPATH");
  482.     char *home = Py_GetPythonHome();
  483. #ifndef _AMIGA
  484.     char *path = getenv("PATH");
  485.     char *prog = Py_GetProgramName();
  486. #endif
  487.     char argv0_path[MAXPATHLEN+1];
  488.     int pfound, efound; /* 1 if found; -1 if found build directory */
  489.     char *buf;
  490.     int bufsz;
  491.     int prefixsz;
  492.     char *defpath = pythonpath;
  493. #ifdef WITH_NEXT_FRAMEWORK
  494.         NSModule pythonModule;
  495. #endif
  496.     
  497. #ifdef WITH_NEXT_FRAMEWORK
  498.         pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
  499.     /* Use dylib functions to find out where the framework was loaded from */
  500.         buf = NSLibraryNameForModule(pythonModule);
  501.         if (buf != NULL) {
  502.             /* We're in a framework. */
  503.             strcpy(progpath, buf);
  504.  
  505.             /* Frameworks have support for versioning */
  506.             strcpy(lib_python, "lib");
  507.         } else {
  508.             /* If we're not in a framework, fall back to the old way (even though NSNameOfModule() probably does the same thing.) */
  509. #endif
  510.     
  511.     /* Initialize this dynamically for K&R C */
  512.     sprintf(lib_python, "lib/python%s", VERSION);
  513.  
  514. #ifdef _AMIGA
  515.     strcpy(progpath,fullprogpath());
  516. #else /* !_AMIGA */
  517.     /* If there is no slash in the argv0 path, then we have to
  518.      * assume python is on the user's $PATH, since there's no
  519.      * other way to find a directory to start the search from.  If
  520.      * $PATH isn't exported, you lose.
  521.      */
  522.     if (strchr(prog, SEP))
  523.         strcpy(progpath, prog);
  524.     else if (path) {
  525.         while (1) {
  526.             char *delim = strchr(path, DELIM);
  527.  
  528.             if (delim) {
  529.                 int len = delim - path;
  530.                 strncpy(progpath, path, len);
  531.                 *(progpath + len) = '\0';
  532.             }
  533.             else
  534.                 strcpy(progpath, path);
  535.  
  536.             joinpath(progpath, prog);
  537.             if (isxfile(progpath))
  538.                 break;
  539.  
  540.             if (!delim) {
  541.                 progpath[0] = '\0';
  542.                 break;
  543.             }
  544.             path = delim + 1;
  545.         }
  546.     }
  547.     else
  548.         progpath[0] = '\0';
  549. #ifdef WITH_NEXT_FRAMEWORK
  550.         }
  551. #endif
  552. #endif /* !_AMIGA */
  553.     strcpy(argv0_path, progpath);
  554.     
  555. #ifdef HAVE_READLINK
  556.     {
  557.         char tmpbuffer[MAXPATHLEN+1];
  558.         int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
  559.         while (linklen != -1) {
  560.             /* It's not null terminated! */
  561.             tmpbuffer[linklen] = '\0';
  562. #ifdef _AMIGA
  563.             if (NULL==strchr(tmpbuffer,':'))
  564. #else
  565.             if (tmpbuffer[0] == SEP)
  566. #endif
  567.                 strcpy(argv0_path, tmpbuffer);
  568.             else {
  569.                 /* Interpret relative to progpath */
  570.                 reduce(argv0_path);
  571.                 joinpath(argv0_path, tmpbuffer);
  572.             }
  573.             linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
  574.         }
  575.     }
  576. #endif /* HAVE_READLINK */
  577.  
  578.     reduce(argv0_path);
  579.  
  580.     if (!(pfound = search_for_prefix(argv0_path, home))) {
  581.         if (!Py_FrozenFlag)
  582.         fprintf(stderr,
  583.            "Could not find platform independent libraries <prefix>\n");
  584.         strcpy(prefix, PREFIX);
  585.         joinpath(prefix, lib_python);
  586.     }
  587.     else
  588.         reduce(prefix);
  589.     
  590.     if (!(efound = search_for_exec_prefix(argv0_path, home))) {
  591.         if (!Py_FrozenFlag)
  592.         fprintf(stderr,
  593.         "Could not find platform dependent libraries <exec_prefix>\n");
  594.         strcpy(exec_prefix, EXEC_PREFIX);
  595.         joinpath(exec_prefix, "lib/lib-dynload");
  596.     }
  597.     /* If we found EXEC_PREFIX do *not* reduce it!  (Yet.) */
  598.  
  599.     if ((!pfound || !efound) && !Py_FrozenFlag)
  600.         fprintf(stderr,
  601.          "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
  602.  
  603.     /* Calculate size of return buffer.
  604.      */
  605.     bufsz = 0;
  606.  
  607.     if (rtpypath)
  608.         bufsz += strlen(rtpypath) + 1;
  609.  
  610.     prefixsz = strlen(prefix) + 1;
  611.  
  612.     while (1) {
  613.         char *delim = strchr(defpath, DELIM);
  614.  
  615. #ifdef _AMIGA
  616.         if (NULL==strchr(defpath,':'))
  617. #else
  618.         if (defpath[0] != SEP)
  619. #endif
  620.             /* Paths are relative to prefix */
  621.             bufsz += prefixsz;
  622.  
  623.         if (delim)
  624.             bufsz += delim - defpath + 1;
  625.         else {
  626.             bufsz += strlen(defpath) + 1;
  627.             break;
  628.         }
  629.         defpath = delim + 1;
  630.     }
  631.  
  632.     bufsz += strlen(exec_prefix) + 1;
  633.  
  634.     /* This is the only malloc call in this file */
  635.     buf = malloc(bufsz);
  636.  
  637.     if (buf == NULL) {
  638.         /* We can't exit, so print a warning and limp along */
  639.         fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
  640.         fprintf(stderr, "Using default static PYTHONPATH.\n");
  641.         module_search_path = PYTHONPATH;
  642.     }
  643.     else {
  644.         /* Run-time value of $PYTHONPATH goes first */
  645.         if (rtpypath) {
  646.             strcpy(buf, rtpypath);
  647.             strcat(buf, delimiter);
  648.         }
  649.         else
  650.             buf[0] = '\0';
  651.  
  652.         /* Next goes merge of compile-time $PYTHONPATH with
  653.          * dynamically located prefix.
  654.          */
  655.         defpath = pythonpath;
  656.         while (1) {
  657.             char *delim = strchr(defpath, DELIM);
  658.  
  659. #ifdef _AMIGA
  660.             if (NULL==strchr(defpath,':')) {
  661. #else
  662.             if (defpath[0] != SEP) {
  663. #endif
  664.                 strcat(buf, prefix);
  665.                 strcat(buf, separator);
  666.             }
  667.  
  668.             if (delim) {
  669.                 int len = delim - defpath + 1;
  670.                 int end = strlen(buf) + len;
  671.                 strncat(buf, defpath, len);
  672.                 *(buf + end) = '\0';
  673.             }
  674.             else {
  675.                 strcat(buf, defpath);
  676.                 break;
  677.             }
  678.             defpath = delim + 1;
  679.         }
  680.         strcat(buf, delimiter);
  681.  
  682.         /* Finally, on goes the directory for dynamic-load modules */
  683.         strcat(buf, exec_prefix);
  684.  
  685.         /* And publish the results */
  686.         module_search_path = buf;
  687.     }
  688.  
  689.     /* Reduce prefix and exec_prefix to their essence,
  690.      * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
  691.      * If we're loading relative to the build directory,
  692.      * return the compiled-in defaults instead.
  693.      */
  694.     if (pfound > 0) {
  695.         reduce(prefix);
  696.         reduce(prefix);
  697.     }
  698.     else
  699.         strcpy(prefix, PREFIX);
  700.  
  701.     if (efound > 0) {
  702.         reduce(exec_prefix);
  703.         reduce(exec_prefix);
  704.         reduce(exec_prefix);
  705.     }
  706.     else
  707.         strcpy(exec_prefix, EXEC_PREFIX);
  708. }
  709.  
  710.  
  711. /* External interface */
  712.  
  713. char *
  714. Py_GetPath()
  715. {
  716.     if (!module_search_path)
  717.         calculate_path();
  718.     return module_search_path;
  719. }
  720.  
  721. char *
  722. Py_GetPrefix()
  723. {
  724.     if (!module_search_path)
  725.         calculate_path();
  726.     return prefix;
  727. }
  728.  
  729. char *
  730. Py_GetExecPrefix()
  731. {
  732.     if (!module_search_path)
  733.         calculate_path();
  734.     return exec_prefix;
  735. }
  736.  
  737. char *
  738. Py_GetProgramFullPath()
  739. {
  740.     if (!module_search_path)
  741.         calculate_path();
  742.     return progpath;
  743. }
  744.